﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace LabelPrintSystem
{
    public class ClsDataUser
    {
        public string sUser { get { return aUser.user; } }
        public string sUserID { set { aUser.userid = value; } get { return aUser.userid; } }
        public string sRemark { get { return aUser.remark; } }
        //public string sUserName{set;get;}
        //public string sUserId { set; get; }
        public string sUserPsw { set; get; }
        public string sUserPow { set; get; }
        public string sError { get; set; }
        public string sEquID { get; set; }

        public clsUser aUser;
        const string connectDB = "ANTENNA";
        const string m_sTableName = "tb_User";

        public ClsDataUser()
        {
            aUser = new clsUser(connectDB);
        }

        public bool verifyUser(string usrid, string pwd)
        {//查询用户有效性
            if (ClsDifinition.isDebug) return true;
            aUser.userid = usrid;
            try
            {
                if (ClsDifinition.RUNNING_MODE == 0)
                {
                    ClsServicePIMAndElecForAntenna Web = new ClsServicePIMAndElecForAntenna();
                    if (!Web.AuthenticateUser(usrid, pwd))
                    {
                        sError = "账号或密码不正确";
                        return false;
                    }
                    aUser.password = pwd;
                    aUser.power = Web.AuthenticateUser_ForPrintReform(usrid);
                    if (!HavePower(clsPowerManage.power.login))
                    {
                        sError = "权限不足！";
                        return false;
                    }
                    return true;
                }
                else
                {
                    ClsUserForMOD Web = new ClsUserForMOD(sEquID);
                    if (!Web.AuthenticateUser(usrid, pwd))
                    {
                        sError = "账号或密码不正确";
                        return false;
                    }
                    aUser.password = pwd;
                    return true;
                }
            }
            catch (Exception ex)
            {
                sError = "服务器异常";
            }

            //try
            //{
            //    SqlParameter param_id = new SqlParameter("@id", usrid);
            //    string sSql = string.Format("select * from [{0}] where UserID = @id", m_sTableName);
            //    DataTable dtUser = MicroDBHelper.ExecuteDataTable(sSql, new SqlParameter[] { param_id }, aUser.m_sConnect);
            //    if (dtUser == null || dtUser.Rows.Count == 0)
            //    {
            //        sError = "员工号不存在!";
            //        return false;
            //    }

            //    if (dtUser.Rows[0][clsUser.chn_Pwd].ToString() == pwd)
            //    {
            //        aUser.userid = dtUser.Rows[0][clsUser.chn_UserID].ToString();
            //        aUser.user = dtUser.Rows[0][clsUser.chn_User].ToString();
            //        aUser.password = dtUser.Rows[0][clsUser.chn_Pwd].ToString();
            //        aUser.power = dtUser.Rows[0][clsUser.chn_Power].ToString();
            //        aUser.remark = dtUser.Rows[0][clsUser.chn_Remark].ToString();
            //        return true;
            //    }
            //    sError = "密码错误!";
            //}
            //catch (Exception ex)
            //{
            //    sError = "服务器异常!";
            //}
            return false;
        }

        public bool HavePower(clsPowerManage.power pow)
        {//获取权限
            return clsPowerManage.getPower(this, pow);
        }

        public bool CangePassword(string strOld, string strNew)
        {//更改密码
            if (verifyUser(sUserID, strOld))
            {
                try
                {
                    string sSql = string.Format("update {0} set [Password] = '{1}' where [UserID] = {2}",
                                         m_sTableName, strNew, sUserID);
                    if (MicroDBHelper.ExecuteNonQuery(sSql, null, aUser.m_sConnect) == 1)
                    {
                        return true;
                    }
                }
                catch { }
            }

            return false;
        }
    }

    public class clsPowerManage
    {//权限管理
        public enum power
        {
            admin = 999,//管理员   
            login = 100,//登录
            config = 101//可设置产品改造和是否免测 及操作打印
        }

        public static bool getPower(ClsDataUser user, power pow)
        {//获取权限
            try
            {
                string[] lstPower = user.aUser.power.Split(',');
                foreach (var p in lstPower)
                {
                    int iPower = Convert.ToInt32(p);
                    if (iPower == (int)power.admin || iPower == (int)pow)
                        return true;
                }
            }
            catch { }

            return false;
        }

        public static string getInfomation()
        {//权限说明
            string info = "权限说明如下，格式为数字，多个权限请用逗号分隔（例： 1,2,3,4,999）" + "\r\n" + "\r\n" +
                          "管理员权限:\t\t999" + "\r\n" +
                          "改造&免测配置权限测试:\t\t1" + "\r\n";
            //"互调手动上传数据:\t2" + "\r\n" +
            //"互调添加配置文件:\t3" + "\r\n" +
            //"互调配置文件可选:\t10" + "\r\n" +
            //"互调DEBUG:\t\t4" + "\r\n" +
            //"电性能测试:\t\t5" + "\r\n" +
            //"电性能手动上传数据:\t6" + "\r\n" +
            //"电性能数据特采:\t\t7" + "\r\n" +
            //"电性能DEBUG:\t\t8";
            //"管理员权限:\t\t30"+"\r\n"+
            //"操作员权限:\t\t31";

            return info;
        }
    }

    public class clsUser
    {//用户信息
        public string m_sConnect;
        public int iColumnNum;
        public string[] m_arrDbHead, m_arrExcelHead;

        public string userid;
        public string user;
        public string password;
        public string power;//权限
        public string remark;

        public static string chn_ID = "ID";
        public static string chn_UserID = "UserID";
        public static string chn_User = "User";
        public static string chn_Pwd = "Password";
        public static string chn_Power = "Power";
        public static string chn_Remark = "Remark";

        public clsUser(string sConnect)
        {//构造
            m_sConnect = sConnect;
            user = "---";
            userid = "---";
            password = "";
            power = "";//权限
            remark = "标签打印员";

            iColumnNum = 6;
            m_arrDbHead = new string[iColumnNum];
            int i = 0;
            m_arrDbHead[i++] = "[" + chn_ID + "]";//
            m_arrDbHead[i++] = "[" + chn_UserID + "]";
            m_arrDbHead[i++] = "[" + chn_User + "]";//
            m_arrDbHead[i++] = "[" + chn_Pwd + "]";//
            m_arrDbHead[i++] = "[" + chn_Power + "]";//
            m_arrDbHead[i++] = "[" + chn_Remark + "]";//

            m_arrExcelHead = new string[iColumnNum];
            i = 0;
            m_arrExcelHead[i++] = "序号";
            m_arrExcelHead[i++] = "员工号";
            m_arrExcelHead[i++] = "用户名";
            m_arrExcelHead[i++] = "密码";
            m_arrExcelHead[i++] = "权限";
            m_arrExcelHead[i++] = "备注";

            //    CREATE TABLE tb_User (
            //    [ID] [int] IDENTITY(1,1) NOT NULL,
            //    [UserID] INT NOT NULL,
            //    [User] NVARCHAR(50) NOT NULL,
            //    [Password] NVARCHAR(50) NOT NULL,
            //    [Power] NVARCHAR(50) NULL DEFAULT NULL,
            //    [Remark] NVARCHAR(50) NULL DEFAULT NULL,
            //)
            //;
        }

        public string GetDbStr(int iStart)
        {//获取SQL  表字段
            if (iStart >= m_arrDbHead.Length)
            {
                return "";
            }
            StringBuilder sbDb = new StringBuilder(256);

            for (int i = iStart; i < m_arrDbHead.Length; i++)
            {
                sbDb.Append(m_arrDbHead[i]);
                if (i != m_arrDbHead.Length - 1)
                {
                    sbDb.Append(", ");
                }
            }
            return sbDb.ToString();
        }
    }
}
